package Json2RDF.Json2Java;

import Json2RDF.Json2Java.OntologyClasses.*;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

public class Json2Java {

  public static List<CylindricalSurface> cylindrical_surface_list;
  public static List<ConicalSurface> conical_surface_list;
  public static List<SphericalSurface> spherical_surface_list;
  public static List<ToroidalSurface> toroidal_surface_list;
  public static List<ToroidalPlane> toroidal_plane_list;
  public static List<Edge> edge_list;
  public static List<PolygonalPlane> polygonal_plane_list;
  public static List<Part> part_list;
  public static List<b_conicalSurface> b_conicalsurface_list;

  public static void JsonExtraction(String FileName) {
    String filePath = FileName;
    StringBuilder jsonContent = new StringBuilder();

    try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
      String line;
      while ((line = br.readLine()) != null) {
        jsonContent.append(line);
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    JSONObject jsonObject = JSON.parseObject(jsonContent.toString());

    List<JSONObject> jsonObjects = IntStream.range(0, 9).mapToObj(i -> JSON.parseObject(jsonContent.toString())).toList();

    long startTime = System.currentTimeMillis();

    ExecutorService executorService= Executors.newFixedThreadPool(9);
    executorService.submit(()->{
      JSONArray cylindrical_surface_array = jsonObjects.get(0).getJSONArray("CYLINDRICAL_SURFACE");
      if (cylindrical_surface_array != null) {
        cylindrical_surface_list = JSON.parseArray(cylindrical_surface_array.toJSONString(), CylindricalSurface.class);
      }
    });
    executorService.submit(()->{
      JSONArray conical_surface_array = jsonObjects.get(1).getJSONArray("CONICAL_SURFACE");
      if (conical_surface_array != null) {
        conical_surface_list = JSON.parseArray(conical_surface_array.toJSONString(), ConicalSurface.class);
      }
    });
    executorService.submit(()->{
      JSONArray spherical_surface_array = jsonObjects.get(2).getJSONArray("SPHERICAL_SURFACE");
      if (spherical_surface_array != null) {
        spherical_surface_list = JSON.parseArray(spherical_surface_array.toString(), SphericalSurface.class);
      }
    });
    executorService.submit(()->{
      JSONArray toroidal_surface_array = jsonObjects.get(3).getJSONArray("TOROIDAL_SURFACE");
      if (toroidal_surface_array != null) {
        toroidal_surface_list = JSON.parseArray(toroidal_surface_array.toString(), ToroidalSurface.class);
      }
    });
    executorService.submit(()->{
      JSONArray toroidal_plane_array = jsonObjects.get(4).getJSONArray("TOROIDAL_PLANE");
      if (toroidal_plane_array != null) {
        toroidal_plane_list = JSON.parseArray(toroidal_plane_array.toString(), ToroidalPlane.class);
      }
    });
    executorService.submit(()->{
      JSONArray edge_array = jsonObjects.get(5).getJSONArray("EDGE");
      if (edge_array != null) {
        edge_list = JSON.parseArray(edge_array.toJSONString(), Edge.class);
      }
    });
    executorService.submit(()->{
      JSONArray polygonal_plane_array = jsonObjects.get(6).getJSONArray("POLYGONAL_PLANE");
      if (polygonal_plane_array != null) {
        polygonal_plane_list = JSON.parseArray(polygonal_plane_array.toJSONString(), PolygonalPlane.class);
      }
    });
    executorService.submit(()->{
      JSONArray part_array = jsonObjects.get(7).getJSONArray("PART");
      if (part_array != null) {
        part_list = JSON.parseArray(part_array.toJSONString(), Part.class);
      }
    });
    executorService.submit(()->{
      JSONArray b_conicalSurface_array = jsonObjects.get(8).getJSONArray("b_conicalSurface");
      if (b_conicalSurface_array != null) {
        b_conicalsurface_list = JSON.parseArray(b_conicalSurface_array.toJSONString(), b_conicalSurface.class);
      }
    });

    executorService.shutdown();
    while (!executorService.isTerminated()) {
    }


/*
    JSONArray cylindrical_surface_array = jsonObject.getJSONArray("CYLINDRICAL_SURFACE");
    if (cylindrical_surface_array != null) {
      cylindrical_surface_list = JSON.parseArray(cylindrical_surface_array.toJSONString(), CylindricalSurface.class);
    }

    JSONArray conical_surface_array = jsonObject.getJSONArray("CONICAL_SURFACE");
    if (conical_surface_array != null) {
      conical_surface_list = JSON.parseArray(conical_surface_array.toJSONString(), ConicalSurface.class);
    }

    JSONArray spherical_surface_array = jsonObject.getJSONArray("SPHERICAL_SURFACE");
    if (spherical_surface_array != null) {
      spherical_surface_list = JSON.parseArray(spherical_surface_array.toString(), SphericalSurface.class);
    }

    JSONArray toroidal_surface_array = jsonObject.getJSONArray("TOROIDAL_SURFACE");
    if (toroidal_surface_array != null) {
      toroidal_surface_list = JSON.parseArray(toroidal_surface_array.toString(), ToroidalSurface.class);
    }

    JSONArray toroidal_plane_array = jsonObject.getJSONArray("TOROIDAL_PLANE");
    if (toroidal_plane_array != null) {
      toroidal_plane_list = JSON.parseArray(toroidal_plane_array.toString(), ToroidalPlane.class);
    }

    JSONArray edge_array = jsonObject.getJSONArray("EDGE");
    if (edge_array != null) {
      edge_list = JSON.parseArray(edge_array.toJSONString(), Edge.class);
    }

    JSONArray polygonal_plane_array = jsonObject.getJSONArray("POLYGONAL_PLANE");
    if (polygonal_plane_array != null) {
      polygonal_plane_list = JSON.parseArray(polygonal_plane_array.toJSONString(), PolygonalPlane.class);
    }

    JSONArray part_array = jsonObject.getJSONArray("PART");
    if (part_array != null) {
      part_list = JSON.parseArray(part_array.toJSONString(), Part.class);
    }*/

    long endTime = System.currentTimeMillis();
    System.out.println(endTime - startTime);

  }

}
